package com.gson.util;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.gson.model.ChResultInfo;
import com.gson.model.OrgInfo;

public class SaxParseUtil extends DefaultHandler {
	
	private String preTag = null;//作用是记录解析时的上一个节点名称
	
	private ChResultInfo resultInfo;
	private List<OrgInfo> orgList = new ArrayList<OrgInfo>();
	private OrgInfo info = null;
	
	public ChResultInfo getSearchResult(String xml) {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser;
		try {
			parser = factory.newSAXParser();
			SaxParseUtil handler = new SaxParseUtil();
			//对字符串解析：
			InputSource is = new InputSource ();
			StringReader xmlStr = new StringReader (xml);
			is.setCharacterStream (xmlStr);
			parser.parse (is,handler);
			return handler.getResultInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ChResultInfo getResultInfo() {
		return resultInfo;
	}
	
	@Override  
    public void startDocument() throws SAXException {
		resultInfo = new ChResultInfo();
    }
	
	@Override  
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {  
        if("SingleResult".equalsIgnoreCase(qName)){
        	info = new OrgInfo(); 
        }
        preTag = qName;//将正在解析的节点名称赋给preTag  
    }  
	
	@Override  
    public void endElement(String uri, String localName, String qName)  
            throws SAXException {  
        if("SingleResult".equalsIgnoreCase(qName)){
        	orgList.add(info);
        	resultInfo.setOrgList(orgList);
        }
        preTag = null;/**当解析结束时置为空。*/  
    }  
	
	String propertyName = null;
	String propertyValue = null;
	
	@Override  
    public void characters(char[] ch, int start, int length) throws SAXException {  
        if(preTag!=null){
            String content = new String(ch,start,length);  
            if("BDCDataSource".equalsIgnoreCase(preTag)){  
            	resultInfo.setSource(content);
            	return;
            }
            
            if("BDCSearchflag".equalsIgnoreCase(preTag)){
            	resultInfo.setSearchFlag(content);
            	return;
            }
            
            if("ResultCount".equalsIgnoreCase(preTag)){
            	resultInfo.setResultCount(content);
            	return;
            }
            
            if("ResultTime".equalsIgnoreCase(preTag)){  
            	resultInfo.setSearchTime(content);
            	return;
            }
            
            if("Reason".equalsIgnoreCase(preTag)){  
            	resultInfo.setReason(content);
            	return;
            }
            
            if("propertyName".equalsIgnoreCase(preTag)){
            	propertyName = content;
            	return;
            }
            
            if("propertyValue".equalsIgnoreCase(preTag)){ 
            	propertyValue = content;
            	
            	if(StringUtil.testParam(propertyName, "产品序列号")) {
            		if(!StringUtil.testParam(propertyValue)){
            			propertyValue = "";
            		}
            		info.setChId(propertyValue);
            	}
            	if(StringUtil.testParam(propertyName, "公司名称")) {
            		if(!StringUtil.testParam(propertyValue)){
            			propertyValue = "";
            		}
            		info.setCustomerName(propertyValue);
            	}
            	if(StringUtil.testParam(propertyName, "地址")) {
            		if(!StringUtil.testParam(propertyValue)){
            			propertyValue = "";
            		}
            		info.setAddress(propertyValue);
            	}
            	if(StringUtil.testParam(propertyName, "首查电话")) {
            		if(!StringUtil.testParam(propertyValue)){
            			propertyValue = "";
            		}
            		info.setCustTelephone(propertyValue);
            	}
            	if(StringUtil.testParam(propertyName, "QYMP")) {
            		if(!StringUtil.testParam(propertyValue)){
            			propertyValue = "";
            		}
            		info.setIsQymp(propertyValue);
            	}
            	if(StringUtil.testParam(propertyName, "ORG_ID")) {
            		if(!StringUtil.testParam(propertyValue)){
            			propertyValue = "";
            		}
            		info.setOrgId(propertyValue);
            	}
            	if(StringUtil.testParam(propertyName, "所属城市")) {
            		if(!StringUtil.testParam(propertyValue)){
            			propertyValue = "";
            		}
            		info.setProvinceAndCity(propertyValue);
            	}
            	if(StringUtil.testParam(propertyName, "Region_Code")) {
            		if(!StringUtil.testParam(propertyValue)){
            			propertyValue = "";
            		}
            		info.setRegionCode(propertyValue);
            	}
            	if(StringUtil.testParam(propertyName, "gpsx")) {
            		if(!StringUtil.testParam(propertyValue)){
            			propertyValue = "";
            		}
            		info.setGpsx(propertyValue);
            	}
            	if(StringUtil.testParam(propertyName, "gpsy")) {
            		if(!StringUtil.testParam(propertyValue)){
            			propertyValue = "";
            		}
            		info.setGpsy(propertyValue);
            	}
            	if(StringUtil.testParam(propertyName, "relevance")) {
            		if(!StringUtil.testParam(propertyValue)){
            			propertyValue = "";
            		}
            		info.setRelevance(propertyValue);
            	}
            	
            	propertyName = null;
            	propertyValue = null;
            	return;
            }
            
        }  
    }
	
}
